package com.example.demo8;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.input.KeyCode;
import java.util.List;

import static com.example.demo8.RPGgame.PLAYER_SIZE;

public class Player {
    private double x, y;
    private double speed;
    private int health;
    private int maxHealth;
    private int damage;
    private boolean isInvincible;
    private int invincibleTimer;
    private double angle;

    private boolean upPressed, downPressed, leftPressed, rightPressed;
    private boolean spacePressed;

    public Player(double x, double y,  int health , double speed, int maxHealth, int damage) {
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.health = health;
        this.maxHealth = maxHealth;
        this.damage = damage;
        this.isInvincible = false;
        this.invincibleTimer = 0;
        this.angle = 0;
    }

    public void update(List<Projectile> projectiles, CollisionSystem collisionSystem) {
        double prevX = x, prevY = y;

        double dx = (leftPressed ? -1 : rightPressed ? 1 : 0);
        double dy = (upPressed ? -1 : downPressed ? 1 : 0);

        double length = Math.sqrt(dx * dx + dy * dy);
        if (length > 0) {
            dx /= length;
            dy /= length;
            angle = Math.atan2(dy, dx);
        }

        x += dx * speed;
        y += dy * speed;

        if (collisionSystem.checkWallCollision(x, y,  PLAYER_SIZE / 2)) {
            x = prevX;
            y = prevY;
        }

        if (spacePressed) {
            projectiles.add(new Projectile(x, y,
                    Math.cos(angle) * 8,
                    Math.sin(angle) * 8,
                    damage,
                    false));
            spacePressed = false;
        }

        if (isInvincible && --invincibleTimer <= 0) {
            isInvincible = false;
        }
    }

    public void handleInput(KeyCode keyCode, boolean isPressed) {
        switch (keyCode) {
            case W -> upPressed = isPressed;
            case S -> downPressed = isPressed;
            case A -> leftPressed = isPressed;
            case D -> rightPressed = isPressed;
            case SPACE -> spacePressed = isPressed;
        }
    }

    public void render(GraphicsContext gc, double playerSize) {
        gc.setFill(isInvincible && invincibleTimer % 10 < 5 ? Color.LIGHTBLUE : Color.BLUE);
        gc.fillOval(x - playerSize / 2, y - playerSize / 2, playerSize, playerSize);

        double indicatorX = x + Math.cos(angle) * playerSize * 0.4;
        double indicatorY = y + Math.sin(angle) * playerSize * 0.4;
        gc.setStroke(Color.WHITE);
        gc.setLineWidth(3);
        gc.strokeLine(x, y, indicatorX, indicatorY);
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public int getHealth() {
        return health;
    }

    public int getMaxHealth() {
        return maxHealth;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public int getDamage() {
        return damage;
    }

    public void setDamage(int damage) {
        this.damage = damage;
    }

    public void setInvincible(boolean invincible) {
        this.isInvincible = invincible;
    }

    public boolean isInvincible() {
        return isInvincible;
    }

    public void setInvincibleTimer(int invincibleTimer) {
        this.invincibleTimer = invincibleTimer;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }
    public double getSpeed() {
        return speed;
    }
}
